package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.core.Result;
import com.dongdongshop.core.ResultEnum;
import com.dongdongshop.pojo.seller.TbBrand;
import com.dongdongshop.service.BrandService;
import com.dongdongshop.util.PageBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("brandController")
public class BrandController {

    private Logger logger = LoggerFactory.getLogger(BrandController.class);

    @Reference
    private BrandService brandService;

    @RequestMapping("toList")
    public String toList(){
        return "admin/brand";
    }

    @RequestMapping("index")
    public String index(){
        return "admin/index";
    }
    //查询
    @RequestMapping("findBrandAll")
    @ResponseBody
    public Result findBarandAll(@RequestParam(defaultValue = "1")Integer pageNumber, @RequestParam(defaultValue = "5")Integer pageSize){
        try {
            PageBean<TbBrand> pageResult = brandService.findBarandAll(pageNumber,pageSize);

            return Result.response(ResultEnum.SUCCESS).setData(pageResult);
        }catch (Exception e){
            logger.error("系统错误",e);
            return Result.response(ResultEnum.ERROR);
        }
    }

    //增加
    @RequestMapping("insertBrandInfo")
    @ResponseBody
    public Result insertBrandInfo(TbBrand brand){
        try {
            brandService.insertBrandInfo(brand);
            logger.info("进入增加方法");
            return Result.response(ResultEnum.SUCCESS);
        }catch (Exception e){
            logger.error(ResultEnum.ERROR.getMessage());
            return Result.response(ResultEnum.ERROR);
        }
    }
    //回显
    @RequestMapping("eachBrandInfo/{id}")
    @ResponseBody
    public Result eachBrandInfo(@PathVariable("id")Long id){
        try {
            TbBrand brand = brandService.eachBrandInfo(id);
            logger.info(ResultEnum.SUCCESS.getMessage());
            return Result.response(ResultEnum.SUCCESS).setData(brand);
        }catch (Exception e){
            logger.error(ResultEnum.ERROR.getMessage());
            return Result.response(ResultEnum.ERROR);
        }
    }
    //修改
    @RequestMapping("updateBrandById")
    @ResponseBody
    public Result updateBrandById(TbBrand brand){
        try {
            brandService.updateBrandById(brand);
            logger.info("修改方法");
            return Result.response(ResultEnum.SUCCESS);
        }catch (Exception e){
            logger.error(ResultEnum.ERROR.getMessage());
            return Result.response(ResultEnum.ERROR);
        }
    }
    //批删
    @RequestMapping("deleteByIds")
    @ResponseBody
    public Result deleteByIds(@RequestParam("arr[]")Long[] arr){
        try {
            brandService.deleteByIds(arr);
            logger.info("删除方法");
            return Result.response(ResultEnum.SUCCESS).setData(arr);
        }catch (Exception e){
            logger.error(ResultEnum.ERROR.getMessage());
            return Result.response(ResultEnum.ERROR);
        }

    }

}
